<?php

namespace Beerus\Modules\Designer\Actions;

use Doraemon\model\DesignerOperatorBind\DesignerOperatorBind;
use Doraemon\model\DesignerAuth;
use Doraemon\service\admin\OperatorBind;
use Doraemon\service\Designer\AdminOperator;
use Illuminate\Http\Request;
use Encore\Admin\Actions\RowAction;
use Illuminate\Database\Eloquent\Model;
use Encore\Admin\Facades\Admin;
use Doraemon\service\admin\SaleBind;

class UnbindOperator extends RowAction
{
    public $name = '取消认领';

    public function handle($id)
    {
        // 获取用户uid
        $uid = DesignerAuth::get('uid', ['id' => $id]);
        if (empty($uid)) {
            return $this->response()->error('未找到该设计师');
        }
        $adminName = Admin::user()->username;
        $model = DesignerOperatorBind::get('*', ['uid' => $uid, 'admin_name' => $adminName]);
        if($model) {
            $res = AdminOperator::unbind($model['uid'], $adminName);
            if($res == AdminOperator::STATUS_SUCCESS) {
                return $this->response()->success('取消认领成功')->refresh();
            } else if($res == AdminOperator::STATUS_REPEAT) {
                return $this->response()->success('已经取消认领,不需要重复操作');
            } else if($res == AdminOperator::STATUS_FAIL_BEBIND) {
                return $this->response()->error('取消认领失败，只有本人才能取消认领');
            } else {
                return $this->response()->error('失败');
            }
        } else {
            return $this->response()->error('未找到该记录1');
        }
    }

    public function retrieveModel(Request $request)
    {
        if (!$key = $request->get('_key')) {
            return false;
        }

        return $key;
    }
}
